﻿using Essensoft.Paylink.WeChatPay;
using Essensoft.Paylink.WeChatPay.V2;
using Essensoft.Paylink.WeChatPay.V2.Notify;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Shop.Core.ERP;
using Shop.Core.Share;
using Shop.Core.YOP;
using Shop.IServices;

namespace Shop.WebApi.Controllers
{
    /// <summary>
    /// 微信支付异步通知
    /// </summary>
    [ApiController]
    [Route("Api/[controller]")]
    [AllowAnonymous]
    public class NotifyController : ShopControllerBase
    {
        private readonly IPaymentAppService _paymentAppServicecs;

        private readonly IOrdersAppService _ordersAppService;

        private readonly IWeChatPayNotifyClient _client;

        private readonly WeChatPayOptions _wechatPay;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="paymentAppServicecs"></param>
        /// <param name="ordersAppService"></param>
        /// <param name="client"></param>
        /// <param name="wechatPay"></param>
        public NotifyController(IPaymentAppService paymentAppServicecs
            , IOrdersAppService ordersAppService
            , IWeChatPayNotifyClient client
            , IOptions<WeChatPayOptions> wechatPay)
        {
            _paymentAppServicecs = paymentAppServicecs;
            _ordersAppService = ordersAppService;
            _client = client;
            _wechatPay = wechatPay.Value;
        }


        /// <summary>
        /// 微信支付回调
        /// </summary>
        /// <returns></returns>
        [HttpPost("WeChatPay")]
        public async Task<IActionResult> Unifiedorder()
        {
            try
            {
                var notify = await _client.ExecuteAsync<WeChatPayUnifiedOrderNotify>(Request, _wechatPay);
                await _paymentAppServicecs.WechatPayNotify(notify);
                return WeChatPayNotifyResult.Success;
            }
            catch (Exception)
            {
                return WeChatPayNotifyResult.Failure;
            }
        }

        /// <summary>
        /// 易宝支付回调
        /// </summary>
        /// <returns></returns>
        [HttpPost("YOPPay")]
        public async Task<ActionResult<object>> YopPayCallBack([FromForm] NotifyDto input)
        {
            var data = await _paymentAppServicecs.YopPayNotify(input);
            if(data.Code == 200)
                return "Success";
            return "Fail";
        }

        /// <summary>
        /// 易宝转账回调
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("YOPTransfer")]
        public async Task<ActionResult<object>> YopTransferCallBack([FromForm] NotifyDto input)
        {
            var data = await _paymentAppServicecs.YopTransferCallBack(input);
            if (data.Code == 200)
                return "Success";
            return "Fail";
        }

        /// <summary>
        /// 物流推送
        /// </summary>
        /// <param name="ts"></param>
        /// <param name="partnerid"></param>
        /// <param name="method"></param>
        /// <param name="sign"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost("ErpCallBack")]
        public async Task<ActionResult<object>> ErpCallBack([FromQuery]string ts, [FromQuery] string partnerid, [FromQuery] string method, [FromQuery] string sign,[FromBody] DeliveryNoticeData data)
        {
            //Console.WriteLine($"ts:{ts}");
            //Console.WriteLine($"partnerid:{partnerid}");
            //Console.WriteLine($"method:{method}");
            //Console.WriteLine($"sign:{sign}");
            //Console.WriteLine($"data:{JsonSerializer.Serialize(data)}");
            await _ordersAppService.Delivery(data);

            return new { code = 0, msg = "执行成功" };
        }
    }
}
